<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\common\UserService;
use app\model\Movie;
use app\model\MovieClass;
use app\model\MovieComment;
use app\model\MovieStyle;
use think\db\Query;

class MovieController extends BaseController
{
	#分类
	function class()
	{
		$rows = MovieClass::select();
		$this->writeJson($rows);
	}
	
	#视频类型
	function style()
	{
		$rows = MovieStyle::select();
		$this->writeJson($rows);
	}
	
	#列表
	function index()
	{
		$class_id = $this->request->get('class_id/d');
		$style_id = $this->request->get('style_id/d');
		$rows = Movie::where(['status' => 6])
			->when(!empty($class_id), function (Query $query) use ($class_id) {
				$query->where('class_id', $class_id);
			})
            ->when(!empty($style_id),function (Query $query) use($style_id){
                $query->whereFindInSet('style_ids',$style_id);
            })
			->visible(['id', 'name', 'verticalimage', 'type'])
			->paginate()
			->items();
		$this->writeJson($rows);
	}
	
	function detail()
	{
		$id = $this->request->get('id/d');
		$row = Movie::where(['id' => $id])
			->with([
				'crew',
				'user_collect' => function (Query $query) {
					$query->where(['user_id' => $this->user_id]);
				},
				'user_like' => function (Query $query) {
					$query->where(['user_id' => $this->user_id]);
				}])
			->append(['style_ids_text'])
			->findOrFail();
		$this->writeJson($row);
	}
	
	function collect()
	{
		$id = $this->request->post('id/d');
		$row = Movie::where(['id' => $id])->findOrFail();
		$ret = UserService::collect($row->id, $this->user_id);
		$this->writeJson($ret);
	}
	
	function like()
	{
		$id = $this->request->post('id/d');
		$row = Movie::where(['id' => $id])->findOrFail();
		$ret = UserService::like($row->id, $this->user_id, 1);
		if ($ret) {
			$row->like += 1;
		} else {
			$row->like -= 1;
		}
		$row->save();
		$this->writeJson($ret);
	}
	
	function comment()
	{
		$id = $this->request->post('id/d');
		$pid = $this->request->post('pid/d', 0);
		$to_user_id = $this->request->post('to_user_id/d', 0);
		$content = $this->request->post('content');
		if (empty($content)) {
			$this->writeJson(0, '评论不能为空');
		}
		$row = Movie::where(['id' => $id])->findOrFail();
		MovieComment::create([
			'movie_id' => $row->id,
			'pid' => $pid,
			'user_id' => $this->user_id,
			'to_user_id' => $to_user_id,
			'content' => $content,
		]);
		$this->writeJson();
	}
	
	#评论点赞
	function commentLike()
	{
		$id = $this->request->post('id/d');
		$row = MovieComment::where(['id' => $id])->findOrFail();
		$ret = UserService::like($row->id, $this->user_id, 4);
		if ($ret) {
			$row->like += 1;
		} else {
			$row->like -= 1;
		}
		$row->save();
		$this->writeJson($ret);
	}
	
	function commentList()
	{
		$id = $this->request->get('id/d');
		$type = $this->request->get('type/d');#1热门  2最新
		$movie = Movie::where(['id' => $id])->findOrFail();
		$rows = MovieComment::where(['movie_id' => $movie->id, 'pid' => 0])
			->withCount(['child'])
			->with([
				'child' => function (Query $query) {
					$query->with([
						'user',
						'to_user',
						'user_like' => function (Query $query) {
							$query->where(['user_id' => $this->user_id]);
						}
					])->limit(3);
				},
				'user',
				'user_like' => function (Query $query) {
					$query->where(['user_id' => $this->user_id]);
				}
			])
			->when(!empty($type), function (Query $query) use ($type) {
				if ($type == 1) {
					$query->order('like', 'desc');
				} else {
					$query->order('id', 'desc');
				}
			})
			->paginate()
			->items();
		$this->writeJson($rows);
	}
	
	#评论详情
	function commentDetail()
	{
		$id = $this->request->get('id/d');
		$rows = MovieComment::where(['pid' => $id])
			->with([
				'user',
				'to_user',
				'user_like' => function (Query $query) {
					$query->where(['user_id' => $this->user_id]);
				}
			])
			->paginate()
			->items();
		$this->writeJson($rows);
	}
	
	#演员
	function crewDetail()
	{
		$id = $this->request->get('id/d');
	}
	
	
	

	

	
}
